﻿$(document).ready(function() {
    GridDemo.Home.Grid2.setupGrid($("#grid2"), $("#pager2"), $("#search2"));
});

GridDemo.Home.Grid2 = {
    setupGrid: function(grid, pager, search) {
        var lastsel;
        grid.jqGrid({
            url: "TwoGrids/List2",
            mtype: "get",
            datatype: "json",
            colNames: ['ProductID', 'ProductName', 'UnitPrice'],
            colModel: [
                        { name: 'ProductID', index: 'ProductID', width: 50, align: 'center', sortable: false, editable: true, hidden: true },
                        { name: 'ProductName', index: 'ProductName', sortable: true, editable: true, hidden: false },
                        { name: 'UnitPrice', index: 'UnitPrice', width: 40, align: 'center', formatter: 'currency', formatoptions: { prefix: "$" }, sortable: true, editable: true, hidden: false }
                      ],
            rowNum: 10,
            rowList: [10, 20, 50],
            pager: pager,
            sortname: 'ProductID',
            sortorder: "asc",
            viewrecords: true,
            multiselect: false,
            onSelectRow: function(id) {
                if (id && id !== lastsel) {
                    //grid.restoreRow(lastsel);
                    grid.saveRow(lastsel, checksave);
                    grid.editRow(id, true);
                    lastsel = id;
                    // lost focus event
                    $("td input[type='text']:enabled").unbind('blur');
                    $("td input[type='text']:enabled").blur(function() {
                        // validate here.
                        var len = $('#1_ProductName').val().length;
                        if (parseInt(len) <= 0)
                            $("#1_ProductName").attr('class', "ui-state-highlight-error");
                        //$('#msg').val('Vui lòng nhập Product Name');
                    });
                    // end lost focus event
                    // key event
                    $("td input[type='text']:enabled").unbind('keyup');
                    $("td input[type='text']:enabled").keyup(function(event) {
                        if (event.keyCode == 40) {  // mui ten xuong
                            var rowindex = id;
                            grid.saveRow(rowindex.toString());
                            rowindex = parseInt(rowindex, 0) + 1;
                            grid.editRow(rowindex.toString());
                            grid.setSelection(rowindex.toString());
                            //window.event.keyCode = 27;
                        }
                        else if (event.keyCode == 38) { // mui ten len
                            var rowindex = id;
                            grid.saveRow(rowindex.toString());
                            rowindex = parseInt(rowindex, 0) - 1;
                            grid.editRow(rowindex.toString());
                            grid.setSelection(rowindex.toString());
                        }
                    });

                    $("td input[type='text']:enabled:last").unbind('keyup');
                    $("td input[type='text']:enabled:last").keyup(function(event) {
                        var rowindex = id;
                        grid.saveRow(rowindex.toString());
                        rowindex = parseInt(rowindex, 0) + 1;
                        grid.setSelection(rowindex.toString());
                        grid.editRow(rowindex.toString());
                    });
                    // end key event
                    return;
                    // autocomplete
                    $('#').autocomplete('TwoGrids/Lookup', {
                        dataType: 'json',
                        parse: function(data) {
                            var rows = new Array();
                            for (var i = 0; i < data.length; i++) {
                                rows[i] = { data: data[i], value: data[i].ProductName, result: data[i].QuantityPerUnit };
                            }
                            return rows;
                        },
                        formatItem: function(row, i, n) {
                            return row.ProductName + ' - ' + row.QuantityPerUnit;
                        },
                        width: 300,
                        mustMatch: true
                    });
                    // end autocomplete
                }
            },
            editurl: "TwoGrids/Create2",
            width: '100%',
            height: '100%',
            autowidth: true,
            rownumbers: true,
            gridview: true,
            caption: 'Grid2'
        })
        .navGrid(pager, { edit: false, add: false, del: false, search: false, refresh: false });
        search.filterGrid("#" + grid.attr("id"), {
            gridModel: false,
            filterModel: [{
                label: 'Search',
                name: 'search',
                stype: 'text'
}]
            });
        }
    };
    function checksave(result) {
        if (result == "") {
            alert("Update is missing!");
            return false;
        }
        return true;
    }
    $().ready(function() {
        $('input').focus(function() {
            $(this).select();
        });
    });

    // cookie - get
    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1)
                    c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }

    // cookie - set
    function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    }
